Already,
let's just get to our new chapter for java!

Classes and Objects


Components of the Class methods:

Headers:

access specifier: public or private
return type: int void double
method name: blah
parameter list:(String password, blah)

Constructor:

1:default constructor

public BankAccount() {
password = "";
balance = 0.0; }

constructs a BankAccount object with a balance of zero and a password equal to the empty string.

2:normal constructor


public BankAccount(String acctPassword, double acctBalance) {
password = acctPassword;
balance = acctBalance; }

Accessor:

accesses a class object without altering the object.
public double getBalance()
{ return balance; }


Mutators:

access and change the variables in the class instance
public void deposit(String acctPassword, double amount) {
if (!acctPassword.equals(password)) /* throw an exception */
else {balance += amount;}

Note that the difference between static method and instance method:
static method and variable are callable throughout the entire scope of the class,
the idea is similar to the global variable that we learned in previous python chapter

METHOD OVERLOADING

Though it might confuse you a little, but it is possible for different method to exist with the same method name, the
system will recognize the difference as long as it has the different reference.
for example in:
public class DoOperations {
public int product(int n) { return n * n; }
public double product(double x) { return x * x; } public double product(int x, int y) { return x * y; }
...
will give the different results

"This" method

this method allow us to clear the ambiguity of variables with identical names
for example:
if we have two variables both have the name balance,

public void deposit(String acctPassword, double balance) {
this.balance += balance; }

This .balance refers to the balance outside this method, is the current balance belongs to the instance

Differentiate primitive and reference data types:

Say if we are writing new instances,
primitive data types creates an entirely new space for the data you input, while reference data types like
class and objects are merely create references instead of actual objects.

Default value in class:

One amazing thing about class is that when initializing attributes it has a default value,
for int is 0 for string is " "